{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "if \"pyodide\" in sys.modules:\n",
    "    import piplite\n",
    "    await piplite.install('pyb2d-jupyterlite-backend>=0.4.2')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from b2d.testbed import TestbedBase\n",
    "import random\n",
    "import numpy\n",
    "import b2d\n",
    "\n",
    "class ColorMixing(TestbedBase):\n",
    "\n",
    "    name = \"ColorMixing\"\n",
    "\n",
    "    def __init__(self, settings=None):\n",
    "        super(ColorMixing, self).__init__(settings=settings)\n",
    "        dimensions = [30, 30]\n",
    "\n",
    "        # the outer box\n",
    "        box_shape = b2d.ChainShape()\n",
    "        box_shape.create_loop(\n",
    "            [\n",
    "                (0, 0),\n",
    "                (0, dimensions[1]),\n",
    "                (dimensions[0], dimensions[1]),\n",
    "                (dimensions[0], 0),\n",
    "            ]\n",
    "        )\n",
    "        box = self.world.create_static_body(position=(0, 0), shape=box_shape)\n",
    "\n",
    "        fixtureA = b2d.fixture_def(\n",
    "            shape=b2d.circle_shape(1), density=2.2, friction=0.2, restitution=0.5\n",
    "        )\n",
    "        body = self.world.create_dynamic_body(position=(13, 10), fixtures=fixtureA)\n",
    "\n",
    "        pdef = b2d.particle_system_def(\n",
    "            viscous_strength=0.9,\n",
    "            spring_strength=0.0,\n",
    "            damping_strength=0.5,\n",
    "            pressure_strength=0.5,\n",
    "            color_mixing_strength=0.008,\n",
    "            density=2,\n",
    "        )\n",
    "        psystem = self.world.create_particle_system(pdef)\n",
    "        psystem.radius = 0.3\n",
    "        psystem.damping = 1.0\n",
    "\n",
    "        colors = [\n",
    "            (255, 0, 0, 255),\n",
    "            (0, 255, 0, 255),\n",
    "            (0, 0, 255, 255),\n",
    "            (255, 255, 0, 255),\n",
    "        ]\n",
    "        posiitons = [(6, 10), (20, 10), (20, 20), (6, 20)]\n",
    "        for color, pos in zip(colors, posiitons):\n",
    "\n",
    "            shape = b2d.polygon_shape(box=(5, 5), center=pos, angle=0)\n",
    "            pgDef = b2d.particle_group_def(\n",
    "                flags=b2d.ParticleFlag.waterParticle\n",
    "                | b2d.ParticleFlag.colorMixingParticle,\n",
    "                # group_flags=b2d.ParticleGroupFlag.solidParticleGroup,\n",
    "                shape=shape,\n",
    "                strength=1.0,\n",
    "                color=color,\n",
    "            )\n",
    "            group = psystem.create_particle_group(pgDef)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyb2d_jupyterlite_backend.async_jupyter_gui import JupyterAsyncGui\n",
    "\n",
    "s = JupyterAsyncGui.Settings()\n",
    "s.resolution = [1000,500]\n",
    "s.scale = 8\n",
    "s.fps = 40\n",
    "\n",
    "tb = b2d.testbed.run(ColorMixing, backend=JupyterAsyncGui, gui_settings=s)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
